﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Loctime
{
    public partial class ActivityListPage : PhoneApplicationPage
    {
        public ActivityListPage()
        {
            InitializeComponent();
            //using (MyDataContext dc = new MyDataContext())
            //{
            //    if (dc.Students.Where(c => c.StuNo == "1").Count() > 0)
            //    {
            //        MessageBox.Show("输入的学号已经存在。"); return;
            //    }

            //    Students stu = new Students()
            //    {
            //        StuNo = "1",
            //        Name = "chaihf",
            //        Email = "@sina"
            //    };

            //    Students stu1 = new Students()
            //    {
            //        StuNo = "2",
            //        Name = "zhangrui",
            //        Email = "@sina11"
            //    };
            //    dc.Students.InsertOnSubmit(stu);
            //    dc.Students.InsertOnSubmit(stu1);
            //    dc.SubmitChanges();
            //}
        }


        // 编辑  
        private void OnDataEdit(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            if (btn != null)
            {
                string no = btn.Tag as string;
                NavigationService.Navigate(new Uri("/ActivitySettingPage.xaml?sno=" + no, UriKind.Relative));
            }
        }

        // 删除数据  
        private void OnDataDelete(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            if (btn != null)
            {
                string sNo = btn.Tag.ToString();
                using (MyDataContext dc = new MyDataContext())
                {
                    //Students is the table name
                    Students stu = dc.Students.FirstOrDefault(s => s.StuNo == sNo);
                    if (stu != null)
                    {
                        dc.Students.DeleteOnSubmit(stu);
                        dc.SubmitChanges();
                        BindList();
                    }
                }
            }
        }

        private void onRefresh(object sender, EventArgs e)
        {
            BindList();
        }

        // 新增  
        private void onNew(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/ActivitySettingPage.xaml", UriKind.Relative));
        }

        /// <summary>  
        /// 把数据绑定到ListBox  
        /// </summary>  
        private void BindList()
        {
            using (MyDataContext dc = new MyDataContext())
            {
                var res =
                    from s in dc.Students
                    select s;
                //stuList is the list box name
                this.stuList.ItemsSource = res.ToList();
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            BindList();
        }
    }
}
